/**
 * Created by ly on 16-4-3 下午1:46
 * Copyright (c) 2016 SAIZ.CN. All rights reserved.
 */

package com.example.adapter;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.Base.BasicSerializable;
import com.example.db.DBSerializable;
import com.example.db.HttpSerializable;


import java.util.ArrayList;
import java.util.List;


public class SimpleListAdapter<T extends BasicSerializable> extends BaseAdapter {
    protected final Activity activity;
    protected final Theme theme;
    protected final Resources resources;
    protected final LayoutInflater inflater;
    private final SparseArray<View> views = new SparseArray<View>();
    private final List<T> itemList = new ArrayList<T>(0);
    private AdapterHolder adapterHolder;

    protected SimpleListAdapter(Activity activity) {
        notNull(activity, "[Assertion failed] - activity is required; it must not be null");
        this.activity = activity;
        this.theme = activity.getTheme();
        this.resources = activity.getResources();
        this.inflater = LayoutInflater.from(activity);
    }

    public void notNull(Object object, String message) {
        if(object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    public SimpleListAdapter(Activity activity, AdapterHolder adapterHolder) {
        this(activity);
        this.adapterHolder = adapterHolder;
    }

    /**
     * 刷新List,重绘View
     *
     * @param itemList 数据
     */
    public final void refresh(List<T> itemList) {
        setItems(itemList);
        notifyDataSetChanged();
    }

    /**
     * 重新加载List,失效View
     *
     * @param itemList 数据
     */
    public final void load(List<T> itemList) {
        setItems(itemList);
        notifyDataSetInvalidated();
    }

    /**
     * 刷新List,重绘View
     *
     * @param itemList 数据
     */
    public final void refreshItems(List<T> itemList) {
        setItems(itemList);
        notifyDataSetInvalidated();
    }

    public final void loadItems(List<T> itemList){
        this.itemList.addAll(itemList);
        notifyDataSetChanged();
    }

    public final void clearItems() {
        if (itemList == null) {
            return;
        }
        itemList.clear();
    }

    public final void removeItem(int position) {
        if (itemList == null) {
            return;
        }
        itemList.remove(position);
    }

    public final void setItems(List<T> itemList) {
        if (itemList == null) {
            return;
        }
        this.itemList.clear();
        this.itemList.addAll(itemList);
    }

    public final void addItem(T item) {
        itemList.add(item);
    }

    public final void replaceItem(int position, T item) {
        if (position < 0) {
            addItem(item);
        } else {
            itemList.set(position, item);
        }
    }

    public final List<T> getAllItem() {
        return new ArrayList<T>(itemList);
    }

    @Override
    public int getCount() {
        if (itemList == null) {
            return 0;
        }
        return itemList.size();
    }

    @Override
    public T getItem(int position) {
        if (itemList == null || position > itemList.size() - 1) {
            return null;
        }
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (itemList == null) {
            return position;
        }
        T t = itemList.get(position);
        if (t == null) {
            return position;
        }
        if (t instanceof DBSerializable) {
            return (Long) t.getId();
        }
        return position;
    }

    public String getItemUUID(int position) {
        if (itemList == null) {
            return null;
        }
        T t = itemList.get(position);
        if (t == null) {
            return null;
        }
        if (t instanceof HttpSerializable) {
            return (String) t.getId();
        }
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = adapterHolder.afterPropertiesSet(inflater);
        }
        AdapterHolder holder = (AdapterHolder) convertView.getTag();
        holder.requestLayout(convertView);
        holder.bindEvent();
        holder.bindValues(position);
        setViews(position, convertView);
        return convertView;
    }

    @Override
    public final void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        views.clear();
    }

    @Override
    public final void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        views.clear();
    }

    public final View getView(int position) {
        return views.get(position);
    }

    protected final void setAdapterHolder(AdapterHolder adapterHolder) {
        this.adapterHolder = adapterHolder;
    }

    /**
     * 获取Dimension资源
     *
     * @param resId 资源ID
     * @return float
     */
    protected final float getDimension(int resId) {
        return resources.getDimension(resId);
    }

    /**
     * 获取Color资源
     *
     * @param resId 资源ID
     * @return color int
     */
    protected final int getColor(int resId) {
        return resources.getColor(resId);
    }

    /**
     * 获取Dimen资源
     *
     * @param resId 资源ID
     * @return Dimen
     */
    protected final int getDimen(int resId) {
        return resources.getDimensionPixelSize(resId);
    }

    /**
     * 获取string资源
     *
     * @param resId 资源ID
     * @return string
     */
    protected final String getString(int resId, Object... var) {
        return resources.getString(resId, var);
    }

    /**
     * 获取drawable资源
     *
     * @param resId 资源ID
     * @return Drawable
     */
    protected final Drawable getDrawable(int resId) {
        return resources.getDrawable(resId);
    }

    private void setViews(int position, View view) {
        views.put(position, view);
    }
}
